package com.example.utils;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.SurfaceControl;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;

public class FragmentTest extends AppCompatActivity {

    Button button,button2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment_test);
        button = findViewById(R.id.add);
        button2 = findViewById(R.id.getFragment);
        initView();
        show(new ItemFragment());
    }

    public void initView(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment1 fragment1 = new Fragment1();
                show(fragment1);
                fragment1.setMessageCallBack(new MessageCallBack() {
                    @Override
                    public void getMessage(String message) {
                        Toast.makeText(FragmentTest.this, message, Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public String sendMessage() {
                        return "I am from Activity";
                    }
                });
            }

        });

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment1 fragment1 =(Fragment1)getSupportFragmentManager().findFragmentById(R.id.Fragment1);
                fragment1.FragmentMethod();
            }
        });

    }


    public void show(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.frameLayout,fragment);
        transaction.commit();
    }

    public void getActivityMethod(){
        Toast.makeText(this, "asd", Toast.LENGTH_SHORT).show();

    }
}